package todolist.helpers;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class dbHelper extends SQLiteOpenHelper {

	public static final String TABLE_USERS = "users";
	public static final String USER_COLUMN_ID = "_id";
	public static final String USER_COLUMN_UNAME = "username";
	public static final String USER_COLUMN_PWORD = "password";

	public static final String TABLE_TASKS = "tasks";
	public static final String TASK_COLUMN_ID = "_id";
	public static final String TASK_COLUMN_USERID = "userid";
	public static final String TASK_COLUMN_LAT = "latitude";
	public static final String TASK_COLUMN_LNG = "longitude";
	public static final String TASK_COLUMN_DUEDATE = "duedate";
	public static final String TASK_COLUMN_SHORTDES = "shortdescription";
	public static final String TASK_COLUMN_LONGDES = "longdescription";
	public static final String TASK_COLUMN_COMPLETED = "completed";
	public static final String TASK_COLUMN_TYPE = "type";
	
	private static final String DATABASE_NAME = "todoList.db";
	private static final int DATABASE_VERSION = 1;

	// Database creation sql statements
	private static final String CREATE_USER_TABLE = 
			"create table " + TABLE_USERS + "( " + 
			USER_COLUMN_ID + " integer primary key autoincrement, " + 
			USER_COLUMN_UNAME+ " text not null, " + 
			USER_COLUMN_PWORD + " text not null );";
	
	private static final String CREATE_TASK_TABLE =
			"create table " + TABLE_TASKS + "( " + 
			TASK_COLUMN_ID + " integer primary key autoincrement, " + 
			TASK_COLUMN_USERID + " integer not null, " + 
			TASK_COLUMN_LAT + " text not null, " +
			TASK_COLUMN_LNG + " text not null, " +
			TASK_COLUMN_DUEDATE + " text not null, " +
			TASK_COLUMN_SHORTDES + " text not null, " +
			TASK_COLUMN_LONGDES + " text not null, " +
			TASK_COLUMN_COMPLETED + " integer, " +
			TASK_COLUMN_TYPE + " text not null );";
	
	public dbHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(CREATE_USER_TABLE);
		database.execSQL(CREATE_TASK_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(dbHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS" + TABLE_USERS + TABLE_TASKS);
		onCreate(db);
	}

}
